package demo.android.myapplication.demos.views

import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
import cn.android.basic.dp
import cn.android.basic.float
import cn.android.basic.sp

/**
 * Created by guoshiwen on 2021/7/10.
 */
class MaterialEditText(context: Context, attrs: AttributeSet?) : AppCompatEditText(context, attrs) {
    var space = 10.dp
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val fm = Paint.FontMetrics()
    var hintSize
        get() = paint.textSize;
        set(value) {
            paint.textSize = value
            paint.getFontMetrics(fm)
            offsetTop = basePaddingTop()
            invalidate()
        }
    var offsetTop = basePaddingTop()
        set(value) {
            field = value
            invalidate()
        }

    var labelAlpha = 0
        set(value) {
            field = value
            paint.alpha = value
            invalidate()
        }

    private val showAnimator = ObjectAnimator.ofPropertyValuesHolder(
        this,
        PropertyValuesHolder.ofInt("offsetTop", 0),
        PropertyValuesHolder.ofInt("labelAlpha", 255)
    )
    private val hideAnimator = ObjectAnimator.ofPropertyValuesHolder(
        this,
        PropertyValuesHolder.ofInt("offsetTop", basePaddingTop()),
        PropertyValuesHolder.ofInt("labelAlpha", 0)
    )

    private var labelShowing = false

    init {
        hintSize = 16.sp
        labelAlpha = 0
        setPadding(paddingLeft, paddingTop + basePaddingTop(), paddingRight, paddingBottom)
    }

    override fun setPadding(left: Int, top: Int, right: Int, bottom: Int) {
        super.setPadding(left, top, right, bottom)
    }

    fun basePaddingTop(): Int {
        return (space + hintSize).toInt()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawText(
            hint, 0, hint.length, paddingLeft.float, offsetTop + -fm.top, paint
        )
    }

    override fun onTextChanged(
        text: CharSequence?,
        start: Int,
        lengthBefore: Int,
        lengthAfter: Int
    ) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter)

        @Suppress("SENSELESS_COMPARISON")
        if (showAnimator == null || hideAnimator == null) {
            return
        }
        if (text?.isEmpty() != false) {
            startHideLabelAnimation()
        } else {
            startShowLabelAnimation()
        }
    }

    private fun startShowLabelAnimation() {
        if (showAnimator.isRunning || labelShowing) return
        if (hideAnimator.isRunning) {
            hideAnimator.cancel()
        }
        showAnimator.start()
        labelShowing = true
    }

    private fun startHideLabelAnimation() {
        if (hideAnimator.isRunning) return
        if(!labelShowing) return
        if (showAnimator.isRunning) {
            showAnimator.cancel()
        }
        hideAnimator.start()
        labelShowing = false
    }

}